use diesel::prelude::*;

use crate::{database::schema, route};

#[derive(Queryable, Selectable)]
#[diesel(table_name = schema::category)]
#[diesel(check_for_backend(diesel::mysql::Mysql))]
pub struct Category {
    pub id: i32,
    pub name: String,
    pub desc: String,
    pub cover: String,
    pub slug: String,
}

#[derive(Insertable, AsChangeset)]
#[diesel(table_name = schema::category)]
pub struct NewCategory<'a> {
    pub name: &'a str,
    pub desc: &'a str,
    pub cover: &'a str,
    pub slug: &'a str,
}

impl<'a> From<&'a route::form::category::NewCategoryForm> for NewCategory<'a> {
    fn from(category: &'a route::form::category::NewCategoryForm) -> Self {
        NewCategory {
            name: &category.name,
            desc: &category.desc,
            cover: &category.cover,
            slug: &category.slug,
        }
    }
}

impl<'a> From<&'a Category> for NewCategory<'a> {
    fn from(category: &'a Category) -> Self {
        NewCategory {
            name: &category.name,
            desc: &category.desc,
            cover: &category.cover,
            slug: &category.slug,
        }
    }
}
